using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class QQSessionView : ChatContainerPage
	{
		private const string TAG = "QQSessionView";
		private const string ScrollingState = "Scrolling";
		private const string NotScrollingState = "NotScrolling";
		private MMObservableCollection<QConversation, QQSessionItem> mDataList = new MMObservableCollection<QConversation, QQSessionItem>();
		private EventWatcher mListChangeWatcher;
		private ScrollViewer mScrollViewer;
		private bool mBIsScrollHandlerSet;
		private ApplicationBar mBar;
		private ApplicationBarIconButton pluginSettingBtn;
		private ApplicationBarIconButton pluginSendMsgBtn;
		private List<UIElement> mUiToHideList;
		internal Grid LayoutRoot;
		internal Grid LayoutContent;
		internal panelTitle2Line panelTitle;
		internal Grid listView;
		internal StackPanel ContentPanel;
		internal MMListBox QQSessionlist;
		internal Grid NoMemberView;
		private bool _contentLoaded;
		public bool IsScrolling
		{
			get;
			private set;
		}
		public QQSessionView()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.QQSessionView_loaded));
			base.add_Unloaded(new RoutedEventHandler(this.QQSessionView_unloaded));
		}
		private void QQSessionView_loaded(object sender, RoutedEventArgs e)
		{
			this.setScrollHandler();
			this.requestHeadImgInCurrView();
		}
		private void QQSessionView_unloaded(object sender, RoutedEventArgs e)
		{
		}
		protected override void OnPageIn(object[] object_0)
		{
			this.InitQQSessionView();
			this.InitWatcher();
		}
		private void InitWatcher()
		{
			if (this.mListChangeWatcher == null)
			{
				this.mListChangeWatcher = new EventWatcher(this, this, new EventHandlerDelegate(this.OnQQConversationChange));
				EventCenter.registerEventWatcher(EventConst.ON_QCONVERSATION_LIST_CHANGE, this.mListChangeWatcher);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_QCONVERSATION_DEL, this.mListChangeWatcher);
			}
		}
		private void UnitWatcher()
		{
			if (this.mListChangeWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_QCONVERSATION_LIST_CHANGE, this.mListChangeWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_QCONVERSATION_DEL, this.mListChangeWatcher);
				this.mListChangeWatcher = null;
			}
		}
		private void OnQQConversationChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			List<QConversation> listObject = evtArgs.getListObject<QConversation>();
			if (listObject == null)
			{
				return;
			}
			using (List<QConversation>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					QConversation current = enumerator.get_Current();
					if (current != null)
					{
						if (evtArgs.mEventID == EventConst.ON_QCONVERSATION_LIST_CHANGE)
						{
							this.ConversationUpdate(current, false);
						}
						else
						{
							if (evtArgs.mEventID == EventConst.ON_STORAGE_QCONVERSATION_DEL)
							{
								Log.smethod_5("QQSessionView", "ConversationDelete In Ui List" + current.strUsrName);
								this.ConversationDelete(current.strUsrName);
							}
						}
					}
				}
			}
		}
		private void InitQQSessionView()
		{
			base.set_ApplicationBar(this.GetApplicationBar());
			Log.smethod_5("QQSessionView", "ServiceCenter.qconversationMgr.getList() start");
			this.ConversationUpdate(ServiceCenter.qconversationMgr.getList());
			Log.smethod_5("QQSessionView", "ServiceCenter.qconversationMgr.getList() end");
			this.mDataList.Bind(this.QQSessionlist);
			if (this.mDataList.get_Count() <= 0)
			{
				Log.smethod_5("QQSessionView", "mDataList.Count <= 0 No Data");
				this.ChangeToNoMsgView();
				return;
			}
			List<string> list = new List<string>();
			for (int i = 0; i < this.mDataList.get_Count(); i++)
			{
				QConversation qConversation = this.mDataList[i];
				list.Add(qConversation.strUsrName);
			}
			list.Add(AccountMgr.getCurAccount().nBindQQ.ToString() + "@qqim");
			BatchGetContactProfileMgr.batchGetProfile(list);
		}
		private void ConversationUpdate(ObservableCollection<QConversation> cvsList)
		{
			if (cvsList == null)
			{
				return;
			}
			using (IEnumerator<QConversation> enumerator = cvsList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					QConversation current = enumerator.get_Current();
					this.ConversationUpdate(current, true);
				}
			}
		}
		public void ConversationUpdate(QConversation qconversation_0, bool isUpdateFromList = false)
		{
			if (qconversation_0 == null)
			{
				return;
			}
			bool flag = true;
			for (int i = 0; i < this.mDataList.get_Count(); i++)
			{
				QConversation qConversation = this.mDataList[i];
				if (qConversation.strUsrName == qconversation_0.strUsrName)
				{
					this.mDataList.RemoveAt(i);
					IL_4D:
					if (flag)
					{
						int num = this.mDataList.get_Count();
						int j = 0;
						while (j < this.mDataList.get_Count())
						{
							QConversation qConversation2 = this.mDataList[j];
							if (qconversation_0.nTime <= qConversation2.nTime)
							{
								j++;
							}
							else
							{
								num = j;
								IL_92:
								this.mDataList.Insert(num, qconversation_0);
								if (this.mDataList.get_Count() > 0 && this.listView.get_Visibility() == 1)
								{
									this.ChangeToListView();
									return;
								}
								return;
							}
						}
						goto IL_92;
					}
					return;
				}
			}
			goto IL_4D;
		}
		public void ConversationDelete(string userName)
		{
			for (int i = 0; i < this.mDataList.get_Count(); i++)
			{
				QConversation qConversation = this.mDataList[i];
				if (qConversation.strUsrName == userName)
				{
					this.mDataList.RemoveAt(i);
					IL_3F:
					if (this.mDataList.get_Count() == 0)
					{
						this.ChangeToNoMsgView();
						QConversationMgr.updateMainConversation();
					}
					return;
				}
			}
			goto IL_3F;
		}
		public void StartChat(string userName, string nickName)
		{
			ChatContainerPage.GoToChatPage(this, userName, null, null, null);
		}
		private void QQSessionItem_ItemClick(object sender, object object_0)
		{
			QConversation qConversation = object_0 as QConversation;
			if (qConversation == null)
			{
				return;
			}
			this.StartChat(qConversation.strUsrName, qConversation.strNickName);
		}
		private void setScrollHandler()
		{
			if (this.mBIsScrollHandlerSet)
			{
				return;
			}
			this.mBIsScrollHandlerSet = true;
			this.mScrollViewer = (ScrollViewer)UIElementTraveler.FindElementRecursive(this.QQSessionlist, typeof(ScrollViewer));
			if (this.mScrollViewer == null)
			{
				return;
			}
			FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this.mScrollViewer, 0) as FrameworkElement;
			if (frameworkElement != null)
			{
				VisualStateGroup visualStateGroup = UIElementTraveler.FindVisualState(frameworkElement, "ScrollStates");
				if (visualStateGroup != null)
				{
					visualStateGroup.add_CurrentStateChanging(new EventHandler<VisualStateChangedEventArgs>(this.OnScrollStateChanging));
				}
			}
		}
		private void OnScrollStateChanging(object sender, VisualStateChangedEventArgs e)
		{
			this.IsScrolling = (e.get_NewState().get_Name() == "Scrolling");
			if (e.get_NewState().get_Name() == "NotScrolling")
			{
				if (this.mScrollViewer != null)
				{
					this.mScrollViewer.ScrollToVerticalOffset(this.mScrollViewer.get_VerticalOffset());
				}
				this.requestHeadImgInCurrView();
				this.QQSessionlist.UpdateLayout();
			}
		}
		private void requestHeadImgInCurrView()
		{
			if (this.mScrollViewer != null && this.mDataList != null)
			{
				Log.smethod_5("QQSessionView", "requestHeadImgInCurrView start");
				int num = 6;
				int num2 = (int)this.mScrollViewer.get_VerticalOffset();
				num2 = num2;
				if (num2 < 0)
				{
					num += num2;
					num2 = 0;
				}
				int i = num2;
				int num3 = 0;
				while (i < this.mDataList.get_Count())
				{
					HeadImgMgr.getHeadImage(this.mDataList[i].strUsrName, true);
					i++;
					num3++;
					if (num3 == num)
					{
						break;
					}
				}
				Log.smethod_5("QQSessionView", "requestHeadImgInCurrView end");
				return;
			}
		}
		public ApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				if (!this.mBar.get_IsVisible())
				{
					this.mBar.set_IsVisible(true);
				}
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Mode(1);
			this.pluginSendMsgBtn = new ApplicationBarIconButton(new Uri("/Source/Plugin/QQOfflineMsg/Resource/QQ_NewMsg.png", 2));
			this.pluginSendMsgBtn.set_Text(strings.Plugin_QQOffline_New);
			this.pluginSendMsgBtn.add_Click(new EventHandler(this.pluginSendMsgBtn_Click));
			this.mBar.get_Buttons().Add(this.pluginSendMsgBtn);
			this.pluginSettingBtn = new ApplicationBarIconButton(new Uri("/Source/Plugin/Common/Resource/Img/Plugin_Setting.png", 2));
			this.pluginSettingBtn.set_Text(strings.Set_Plugin_Setting);
			this.pluginSettingBtn.add_Click(new EventHandler(this.pluginSettingButton_Click));
			this.mBar.get_Buttons().Add(this.pluginSettingBtn);
			return this.mBar;
		}
		private void pluginSendMsgBtn_Click(object sender, EventArgs e)
		{
			string text = "QQOfflineMsgPulgin";
			ToolFunction.GotoPageForUserControl("/Source/Plugin/Common/QQInfo/UI/ViewQQGroups.xaml", new object[]
			{
				text
			}, false);
		}
		private void pluginSettingButton_Click(object sender, EventArgs e)
		{
			base.GoToPage("/Source/Plugin/PluginSettingPage.xaml", new object[]
			{
				PluginManager.findPluginByName("QQOfflineMsg")
			}, false);
			int i = 0;
			while (i < PageManager.getInstance().basePageList.get_Count())
			{
				if (!(PageManager.getInstance().basePageList.get_Item(i) is PluginSettingPage))
				{
					i++;
				}
				else
				{
					PluginSettingPage pluginSettingPage = PageManager.getInstance().basePageList.get_Item(i) as PluginSettingPage;
					if (pluginSettingPage.GetPluginName() == "QQOfflineMsg")
					{
						PageManager.getInstance().PopPage();
						return;
					}
					return;
				}
			}
		}
		private void ChangeToNoMsgView()
		{
			if (this.listView.get_Visibility() == 1)
			{
				return;
			}
			this.listView.set_Visibility(1);
			this.NoMemberView.set_Visibility(0);
		}
		private void ChangeToListView()
		{
			if (this.NoMemberView.get_Visibility() == 1)
			{
				return;
			}
			this.NoMemberView.set_Visibility(1);
			this.listView.set_Visibility(0);
		}
		public override List<UIElement> GetBkContents()
		{
			if (this.mUiToHideList == null)
			{
				this.mUiToHideList = new List<UIElement>();
				this.mUiToHideList.Add(this.LayoutContent);
			}
			return this.mUiToHideList;
		}
		public override Panel GetChatContainerPanel()
		{
			return this.LayoutRoot;
		}
		protected override void resumeFromChat()
		{
			this.QQSessionlist.Focus();
			UIDelayService.getInstance().DoDelayAction();
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (base.PreOnBackKeyPress(cancelEventArgs_0))
			{
				return;
			}
			base.OnBackKeyPress(cancelEventArgs_0);
		}
		protected override void OnPageOut()
		{
			base.PreOnPageOut();
			this.UnitWatcher();
			if (this.mDataList != null && Enumerable.Count<QConversation>(this.mDataList) != 0)
			{
				QConversationMgr.updateMainConversation();
			}
		}
		protected override void OnPageForward(Uri targetPage)
		{
			base.PreOnPageForward(targetPage);
		}
		protected override void OnPageBack()
		{
			base.PreOnPageBack();
		}
		protected override bool IsAlertMsgNeedShow(ChatMsg chatMsg_0)
		{
			bool result = true;
			if (base.PreIsAlertMsgNeedShow(chatMsg_0, ref result))
			{
				return result;
			}
			return !chatMsg_0.strTalker.EndsWith("@qqim");
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/QQOfflineMsg/UI/QQSessionView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.LayoutContent = (Grid)base.FindName("LayoutContent");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.listView = (Grid)base.FindName("listView");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.QQSessionlist = (MMListBox)base.FindName("QQSessionlist");
			this.NoMemberView = (Grid)base.FindName("NoMemberView");
		}
	}
}
